package ita.class1.group2.supermarket.common.jms;

import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.captain.model.JmsConfiguration;
import ita.class1.group2.supermarket.common.util.FileUtil;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import javax.jms.*;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JmsManager {

    private static final Logger LOGGER = Logger.getLogger(JmsManager.class);
    private static final String JMS_CONFIGURATION_FILE = "/jmsconf.json";

    private List<JmsConfiguration> jmsConfigurations = null;
    private Map<String, ConnectionFactory> connectionFactoryMap = null;

    public JmsManager() {
        initializeJmsConfiguration();
        initializeJmsConnectionFactories();
    }

    private void initializeJmsConnectionFactories() {
        this.connectionFactoryMap = new HashMap<String, ConnectionFactory>();
        for (JmsConfiguration configuration: this.jmsConfigurations) {
            LOGGER.info("Initialize JMS connection factories: " + configuration.toString());
            this.connectionFactoryMap.put(configuration.getUrl(), new ActiveMQConnectionFactory(configuration.getUrl()));
        }
    }

    public Connection getConnection(String url) {
        try {
            return this.connectionFactoryMap.get(url).createConnection();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void initializeJmsConfiguration() {
        LOGGER.info("Initialize JMS configuration");
        try {
            this.jmsConfigurations = JsonUtil.json2list(FileUtil.readFile(new File(JmsManager.class.getResource(JMS_CONFIGURATION_FILE).getFile())), JmsConfiguration.class);
        } catch (Exception e) {
            LOGGER.error("Can not load JMS configurations, Message: " + e.toString());
            e.printStackTrace();
        }
    }

    public Map<String, ConnectionFactory> getConnectionFactoryMap() {
        return connectionFactoryMap;
    }

    public List<JmsConfiguration> getJmsConfigurations() {
        return jmsConfigurations;
    }

    public void close(MessageProducer producer, Session session, Connection connection) {
        try {
            if (producer != null) producer.close();
            close(session, connection);
        } catch(JMSException e) {
            e.printStackTrace();
        }
    }

    public void close(MessageConsumer consumer, Session session, Connection connection) {
        try {
            if (consumer != null) consumer.close();
            close(session, connection);
        } catch(JMSException e) {
            e.printStackTrace();
        }
    }

    private void close(Session session, Connection connection) throws JMSException {
        if (session != null) session.close();
        if (connection != null) connection.close();
    }

}
